package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.gmall.product.service.BaseTrademarkService;

import org.springframework.web.bind.annotation.RestController;

/**
 * 品牌表 前端控制器
 *
 * @author atguigu
 * @since 2022-12-24
 */
@Api(tags = "品牌表控制器")
@RestController
@RequestMapping("/admin/product/baseTrademark")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;


    /**
     * 品牌分页查询
     *
     * @param page  页码
     * @param limit 页大小
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<IPage<BaseTrademark>> baseTrademarkByPage(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit) {
        //1.封装分页请求参数Page - 分页对象只有页大小跟页码
        IPage<BaseTrademark> iPage = new Page<>(page, limit);
        //2.调用业务逻辑实现分页查询 执行完业务分页后 数据集合，总记录数等属性有值
        iPage = baseTrademarkService.baseTrademarkByPage(iPage);
        return Result.ok(iPage);
    }


    /**
     * 根据品牌ID 查询品牌对象
     *
     * @param id 品牌ID
     * @return
     */
    @GetMapping("/get/{id}")
    public Result<BaseTrademark> getById(@PathVariable("id") Long id) {
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }


    /**
     * 修改品牌信息
     * @param baseTrademark
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }


    /**
     * 品牌删除
     * @param id 品牌ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        baseTrademarkService.removeById(id);
        return Result.ok();
    }

    /**
     * 品牌保存
     * @param baseTrademark
     * @return
     */
    @PostMapping("/save")
    public Result saveTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

}
